# -*- coding: utf-8 -*-

import tkFileDialog
import tkMessageBox
import sys
import xmlrpclib

from xTkinter import *

class Vue_Facture(Frame):
    def __init__(self,master,parent,**cnf):
        Frame.__init__(self,master,cnf)        
        self.initServeur()
        self.parent =parent
        f=Frame(master)
        self.initServeur()
        self.listId=[]
        self.grandTotal=[]
        self.combo=xCombobox(self,width=20)
        self.combo.setLocation(10,10)
        self.combo.setListWidth(20)
        self.combo.setListHeight(5)

        self.combo.setValidator(self.choix)
        

        self.configure(width=550,height=700)
        
        requet="select * from s_customer"
        rep=self.serveur.ExecuterRequeteSQLWhere(requet)
        
        if(rep[0]==1):
            list =rep[1]
            for i in list:              
                self.combo.addItem(str(i[0]))
        else:
            print "reponse",self.rep[1]
            
        self.combo.setText("Id Client")
            

##============================================================
        text2=Label(self,text="Numéro")
        text2.place(x=10,y=55 )
        self.entry2 = Entry(self,width=10,bg='#F7F2B2')
        self.entry2.place(x=120,y=55)
        
        
        text3=Label(self,text="Nom").place(x=10,y=80 )
        self.entry3 = Entry(self,width=30,bg='#F7F2B2')
        self.entry3.place(x=120,y=80)
        
        text4=Label(self,text="Téléphone").place(x=10,y=100 )
        self.entry4 = Entry(self,width=20,bg='#F7F2B2')
        self.entry4.place(x=120,y=100)
        
        text5=Label(self,text="Adresse").place(x=10,y=120 )
        self.entry5 = Entry(self,width=60,bg='#F7F2B2')
        self.entry5.place(x=120,y=120)
        
        text6=Label(self,text="Ville").place(x=10,y=140 )
        self.entry6 = Entry(self,width=25,bg='#F7F2B2')
        self.entry6.place(x=120,y=140)
        
        text7=Label(self,text="Province").place(x=280,y=140 )
        self.entry7 = Entry(self,width=20,bg='#F7F2B2')
        self.entry7.place(x=340,y=140)
        
        text8=Label(self,text="Pays").place(x=10,y=160 )
        self.entry8 = Entry(self,width=20,bg='#F7F2B2')
        self.entry8.place(x=120,y=160)
        
        text9=Label(self,text="Code Postal").place(x=264,y=160 )
        self.entry9 = Entry(self,width=15,bg='#F7F2B2')
        self.entry9.place(x=340,y=160)
        
        text10=Label(self,text="Crédit").place(x=10,y=180 )
        self.entry10 = Entry(self,width=15,bg='#F7F2B2')
        self.entry10.place(x=120,y=180)
        
        text11=Label(self,text="ID du Représentant").place(x=10,y=200 )
        self.entry11 = Entry(self,width=10,bg='#F7F2B2')
        self.entry11.place(x=120,y=200)
        
        text11=Label(self,text="ID de la Région").place(x=10,y=220 )
        self.entry11 = Entry(self,width=10,bg='#F7F2B2')
        self.entry11.place(x=120,y=220)

        self.place(x=0,y=0)
##================================================================

    def choix(self):
        self.entry2.delete(0,END)
        self.entry3.delete(0,END)
        self.entry4.delete(0,END)
        self.entry5.delete(0,END)
        self.entry6.delete(0,END)
        self.entry7.delete(0,END)
        self.entry8.delete(0,END)
        self.entry9.delete(0,END)
        self.entry10.delete(0,END)
        self.compteur=380
        x=self.combo.getText()
        self.entry2.insert(0,x)
        requet1="select * from s_customer where id="+x
        rep1=self.serveur.ExecuterRequeteSQLWhere(requet1)
        
        if(rep1[0]==1):
            list1 =rep1[1]
            if list1[0][1]:
                self.entry3.insert(0,list1[0][1])
            if list1[0][2]:
                self.entry4.insert(0,list1[0][2])
            if list1[0][3]:
                self.entry5.insert(0,list1[0][3])
            if list1[0][4]:
                self.entry6.insert(0,list1[0][4])
            if list1[0][5]:
                self.entry7.insert(0,list1[0][5])
            if list1[0][6]:
                self.entry8.insert(0,list1[0][6])
            if list1[0][7]:
                self.entry9.insert(0,list1[0][7])
            if list1[0][8]:
                self.entry10.insert(0,list1[0][8])
            if list1[0][9]:
                self.entry11.insert(0,list1[0][9])


            self.Bouton=Button(self,text="Creer la commande",width=20, command=self.creerFacture)
            self.Bouton.place(x=220,y=50)

             
        else:
            print "reponse",self.rep1[1]
 
    def initServeur(self):        
        PortServeur=':8000'
        Serveur='http://localhost:8000'
        self.serveur=xmlrpclib.ServerProxy(Serveur)
        pass 
    
    def creerFacture(self):
        Button(self,text="Ajouter un article",width=20, command=self.creerFacture).place(x=200,y=300)
        Button(self,text="Calculer Facture",width=20, command=self.total).place(x=400,y=300)        
        text11=Label(self,text="FACTURE").place(x=10,y=300 )
        self.combo1=xCombobox(self,width=20)
        self.combo1.setLocation(10,330)
        self.combo1.setListWidth(20)
        self.combo1.setListHeight(10)

        self.combo1.setValidator(self.choix1)
        requet2="select * from s_product"
        rep=self.serveur.ExecuterRequeteSQLWhere(requet2)
        
        if(rep[0]==1):
            list =rep[1]
            for i in list:              
                self.combo1.addItem(str(i[0]))
        else:
            print "reponse",self.rep[1]
        
        self.combo1.setText("Id Produit")
        
        text13=Label(self,text="ID du produit").place(x=10,y=360 )
        self.entry13 = Entry(self,width=13)
        self.entry13.place(x=10,y=self.compteur)
        text14=Label(self,text="Nom du produit").place(x=120,y=360 )
        self.entry14 = Entry(self,width=25)
        self.entry14.place(x=100,y=self.compteur)
        text15=Label(self,text="Decription").place(x=260,y=360 )
        self.entry15 = Entry(self,width=22)
        self.entry15.place(x=260,y=self.compteur)
        text16=Label(self,text="Prix").place(x=405,y=360 )
        self.entry16 = Entry(self,width=6)
        self.entry16.place(x=405,y=self.compteur)
        text17=Label(self,text="Qté").place(x=450,y=360 )
        self.entry17 = Entry(self,width=5)
        self.entry17.place(x=450,y=self.compteur)
        self.entry17.bind('<Return>',self.nextItem)
        text18=Label(self,text="Total").place(x=490,y=360 )
        self.total=IntVar()
        self.total.set(0)
        self.entry18 = Entry(self,textvariable=self.total,width=15)        
        self.entry18.place(x=490,y=self.compteur) 
   
        self.compteur=self.compteur+25
        
    def choix1(self):
        self.entry13.delete(0,END)
        self.entry14.delete(0,END)
        self.entry15.delete(0,END)
        self.entry16.delete(0,END)
        self.entry17.delete(0,END)
        y=self.combo1.getText()
        requet3="select * from s_product where id="+y
        rep3=self.serveur.ExecuterRequeteSQLWhere(requet3)
        if(rep3[0]==1):
            list3 =rep3[1]
            if list3[0][1]:
                self.entry13.insert(0,list3[0][0])
            if list3[0][2]:
                self.entry14.insert(0,list3[0][1])
            if list3[0][3]:
                self.entry15.insert(0,list3[0][1])
            if list3[0][4]:
                self.entry16.insert(0,list3[0][5])
            if list3[0][6]:
                self.entry18.insert(0,list3[0][6])
                
 
    def nextItem(self,evt):
        self.entry18.focus()
        x=float(self.entry17.get())
        tempo=self.entry16.get()
        te=""
           
        i=int(tempo[:tempo.index('.')])        
        d=float(tempo[tempo.index('.')+1:])        
        y=float(i+float(d/100))
        self.grandTotal.append(x*y)
        self.total.set(x*y)
        
        pass

    def total(self):
        total=0
        textTotal=Label(self,text="Sous Total").place(x=350,y=self.compteur+25 )
        for i in range(len(self.grandTotal)):
            total+=self.grandTotal[i]   
        varTotal=str(total)  
        self.entryTotal = Entry(self,text=varTotal,width=20)
        self.entryTotal.place(x=450,y=self.compteur+25)
        self.entryTotal.insert(0,varTotal)
        pass


if __name__ == '__main__':
    root=Tk()
    root.geometry("600x700")
    root.title("PERP | Calcul Facture")
    Client=VueFacture(root,None)
    root.mainloop()